package com.mtw.bbs.like.job;

import com.mtw.bbs.constant.LikeOperateEnum;
import com.mtw.bbs.like.pojo.dto.LikeCountDto;
import com.mtw.bbs.like.pojo.dto.LikeRecordDto;
import com.mtw.bbs.like.service.LikeCountService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * 异步任务
 */
@Component
@RequiredArgsConstructor
public class LikeAsyncJob {


    private final LikeCountService likeCountService;
    private final ThreadPoolTaskExecutor asyncExecutor;



    public void doAsyncJob(LikeOperateEnum result,LikeRecordDto dto){
        // TODO 发送点赞消息
        this.updateLikeCount(result,dto);
    }




    /**
     * 更新点赞统计
     */
    private void updateLikeCount(LikeOperateEnum likeOperateEnum, LikeRecordDto dto){

        LikeCountDto countDto = new LikeCountDto();
        countDto.setObjectId(dto.getObjectId());
        countDto.setObjectType(dto.getObjectType());
        countDto.setOperate(likeOperateEnum);
        CompletableFuture.runAsync(()->likeCountService.addLikeCount(countDto),asyncExecutor);
    }
}
